package com.fjec.paid.rabbitmq.consumer;

import com.fjec.common.protocol.FjecFruitsProto;
import com.fjec.common.util.DateUtil;
import com.fjec.common.util.SequenceIdGenerator;
import com.fjec.paid.entity.BizOrderPayment;
import com.fjec.paid.service.IBizOrderPaymentService;
import com.fjec.paid.service.PhysicalPreparePayService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
@Slf4j
public class PhysicalPreparePayConsumer {

    @Autowired
    private PhysicalPreparePayService physicalPreparePayService;

    @Resource
    private IBizOrderPaymentService bizOrderPaymentService;

    @Autowired
    private SequenceIdGenerator idGenerator;

    /**
     * 实物商品支付consumer
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(
                            value = "${fjec.paid.preparepay.queue.name}",
                            durable = "${fjec.paid.preparepay.queue.durable}"
                    ),
                    exchange = @Exchange(
                            value = "${fjec.paid.preparepay.exchange.name}",
                            durable = "${fjec.paid.preparepay.exchange.durable}",
                            type = "${fjec.paid.preparepay.exchange.type}",
                            ignoreDeclarationExceptions = "${fjec.paid.preparepay.exchange.ignoreDeclarationExceptions}"
                    ),
                    key = "${fjec.paid.preparepay.router.key}"
            )
    )
    @RabbitHandler
    public void onPreparePay(Message message, Channel channel, @Headers Map<String, Object> headers) throws Exception {
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            FjecFruitsProto.FjecFruitsMsg fjecFruitsMsg = FjecFruitsProto.FjecFruitsMsg.parseFrom(message.getBody());
            if (fjecFruitsMsg.hasPreparePay()) {
                FjecFruitsProto.PreparePay preparePay = fjecFruitsMsg.getPreparePay();
                String subOpenId = preparePay.getSubOpenId();
                String outTradeNo = preparePay.getOutTradeNo();
                String shopDate = preparePay.getShopDate();
                String subject = preparePay.getSubject();
                String totalAmount = preparePay.getTotalAmount();
                String sellerId = preparePay.getSellerId();
                String sellerName = preparePay.getSellerName();
                Integer paymentType = preparePay.getPaymentType();
                // 订单支付表 无论获取jsapi info是否成功都需记录，故在事务外执行
                BizOrderPayment bizOrderPayment = new BizOrderPayment();
                bizOrderPayment.setId(idGenerator.nextId());
                bizOrderPayment.setPaymentType(paymentType);
                bizOrderPayment.setPaymentMode(1);
                bizOrderPayment.setAmount((int) (Double.parseDouble(totalAmount) * 100));
                bizOrderPayment.setSummary("payment");
                bizOrderPayment.setPaymentStatus(0);
                bizOrderPayment.setDelFlag(0);
                bizOrderPayment.setCreateTime(DateUtil.nowLdt());
                bizOrderPaymentService.save(bizOrderPayment);

                physicalPreparePayService.handle(subOpenId, outTradeNo, shopDate, subject, totalAmount, sellerId, sellerName, bizOrderPayment, paymentType);
                channel.basicAck(deliveryTag, true);
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            channel.basicReject(deliveryTag, false);
        }
    }
}
